package com.government.common.utils;

import cn.hutool.core.util.RandomUtil;
import org.springframework.context.MessageSource;
import org.springframework.context.i18n.LocaleContextHolder;
import com.government.common.utils.spring.SpringUtils;

/**
 * 获取i18n资源文件
 * 
 * @author system
 */
public class MessageUtils
{

    private static final String MONOMER_IDENTIFIER = "#{[%s]}";

    /**
     * 根据消息键和参数 获取消息 委托给spring messageSource
     *
     * @param code 消息键
     * @param args 参数
     * @return 获取国际化翻译值
     */
    public static String message(String code, Object... args)
    {
        MessageSource messageSource = SpringUtils.getBean(MessageSource.class);
        return messageSource.getMessage(code, args, LocaleContextHolder.getLocale());
    }



    public static String verifyCode() {
        return RandomUtil.randomNumbers(6);
    }

    public static String contextSearch(String context, Object... param) {
        for (int i = 0;i<param.length;i++){
            String format = String.format(MONOMER_IDENTIFIER, i);
            context = context.replace(format,param[i].toString());
        }
        return context;
    }
}
